package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.MenuDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;
    @Override
    public List<Menu> findByName(String username) {
        int level = 1;
        List<Menu> menus=menuDao.selectByName(username,level);
        List<Menu>  childrenMenu=new ArrayList<>();
        for (Menu menu : menus) {
            int level2 =2;
            if (null!=menu.getId()) {
                childrenMenu = menuDao.selectByID(username, level2,menu.getId());
                menu.setChildren(childrenMenu);
            }
        }
        log.info("[menu]:{}",menus);
        return menus;
    }

    @Override
    public Menu findMenuByName(String username) {
        Menu menu = menuDao.selectMenu(username,2,1);
        return menu;
    }



;
    @Override
    public List<Menu> findAll() {
        return menuDao.selectAll();
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        log.info("[检查组-分页查询]data：{}",queryPageBean);
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //查询page
        Page<Menu> page = menuDao.selectByCondition(queryPageBean.getQueryString());
        //封装返回值
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(Menu menu) {
        menuDao.insert(menu);
    }

    @Override
    public void edit(Menu Menu) {
        menuDao.update(Menu);
    }

    @Override
    public Menu findById(Integer id) {
        return menuDao.selectById(id);
    }

    @Override
    public void delete(Integer id) {
        menuDao.delete(id);
    }
}
